package com.legal.controller.chat;


import com.legal.domain.dto.ChatHistory;
import com.legal.domain.result.Result;
import com.legal.domain.vo.MessageVO;
import com.legal.service.chat.ChatHistoryService;
import com.legal.service.chat.ChatMessageService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/ai/history")
@RestController
@RequiredArgsConstructor
@Tag(name = "历史记录相关接口")  // OpenAPI 3.0 注解
public class ChatHistoryController {
    private final ChatHistoryService chatHistoryService;

    private final ChatMessageService chatMessageService;

    /**
     * 返回对话历史记录列表
     * 根据用户编号userNo 和对话类型获取用户的所有对话历史记录
     * @param chatHistory
     * @return
     */
    @PostMapping("/list")
    public Result<List<ChatHistory>>  getChatHistories(@RequestBody ChatHistory chatHistory){
        List<ChatHistory> chatHistories = chatHistoryService.getChatHistories(chatHistory.getType(), chatHistory.getUserNo());
        return Result.ok(chatHistories);
    }

    /**
     * 返回某个对话历史记录详情
     * 根据对话id获取用户某个对话的历史记录
     * @param chatId
     * @return
     */
    @GetMapping("/{chatId}")
    public Result<List<MessageVO>> getChatMessage(@PathVariable("chatId") String chatId){
        List<MessageVO> chatHistory = chatMessageService.getChatHistory(chatId);
        return Result.ok(chatHistory);
    }

    /**
     * 删除用户某个对话的历史记录
     * @param chatHistory
     * @return
     */
    @DeleteMapping("/delete")
    public Result<Void> deleteChatHistory(@RequestBody ChatHistory chatHistory){
        //只需要type, userNo, chatId
        chatHistoryService.deleteChatHistory(chatHistory.getType(), chatHistory.getUserNo(), chatHistory.getChatId());
        return Result.ok();
    }

    /**
     * 修改用户某个对话的历史记录（标题）
     * @param chatHistory
     * @return
     */
    @PutMapping("/edit")
    public Result<Void> editChatHistory(@RequestBody ChatHistory chatHistory){
        //type, userNo, chatId, title
        chatHistoryService.editChatHistory(chatHistory.getType(), chatHistory.getUserNo(), chatHistory.getChatId(), chatHistory.getTitle());
        return Result.ok();
    }


}
